//
//  QHKeyboard.m
//  QHKeyboard
//
//  Created by 王庆华 on 16/11/24.
//  Copyright © 2016年 王庆华. All rights reserved.
//

#import "QHKeyboard.h"
#import "QHKeyboardNumPad.h"

#define  iPhone4     ([[UIScreen mainScreen] bounds].size.height==480)
#define  iPhone5     ([[UIScreen mainScreen] bounds].size.height==568)
#define  iPhone6     ([[UIScreen mainScreen] bounds].size.height==667)
#define  iPhone6plus ([[UIScreen mainScreen] bounds].size.height==736)


@interface QHKeyboard ()<QHKeyboardNumPadDelegate>

@property (nonatomic, weak) QHKeyboardNumPad *numPad;

@end

@implementation QHKeyboard

- (instancetype)init {
   
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:1];
        CGRect rect = CGRectZero;
        if (iPhone4 || iPhone5) {
            //            rect = CGRectMake(0, 0, 320, 180);
            rect = CGRectMake(0, 0, 320, 216);
        }else if (iPhone6){
            //            rect = CGRectMake(0, 0, 375, 375/320*180);
            rect = CGRectMake(0, 0, 375, 216);
        }else{
            //            rect = CGRectMake(0, 0, 414, 414/320*180);
            rect = CGRectMake(0, 0, 414, 226);
        }
        
        self.frame = rect;
        QHKeyboardNumPad *numPad = [[QHKeyboardNumPad alloc] initWithFrame:rect];
        numPad.delegate = self;
        [self addSubview:numPad];
        self.numPad = numPad;
        
        //监听当键盘将要出现时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        //监听当键将要退出时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    return self;
}


- (void)showOnView:(UIView *)view
{
    self.isShow = YES;
    self.y = kScreenHeight;
    
    [view addSubview:self];
    [view bringSubviewToFront:self];
    
    if (self.toolBar) {
        [view addSubview:self.toolBar];
    }
    [self creatShowAnimation];
}


- (void)showOnWindow
{
    
    self.isShow = YES;
    self.y = kScreenHeight;
    
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [rootWindow bringSubviewToFront:self];
    
    if (self.toolBar) {
        [rootWindow addSubview:self.toolBar];
    }
    [self creatShowAnimation];
}

- (void)dissmiss
{
    
    self.isShow = NO;
    [self.toolBar endEditing:YES];
    [UIView animateWithDuration:0.2 animations:^{
        
        self.y = kScreenHeight;
        if (self.toolBar) {
            self.toolBar.y = kScreenHeight - self.inputView.height;
        }
    } completion:^(BOOL finished) {
        self.hidden = YES;
        if (self.toolBar) {
            self.toolBar.hidden = YES;
            [self.toolBar removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}


- (void)creatShowAnimation
{
    self.hidden = NO;
    if (self.toolBar) {
        self.toolBar.hidden = NO;
        self.toolBar.y = kScreenHeight;
    }
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView animateWithDuration:0.2 animations:^{
        
        self.y = kScreenHeight - self.height;
        if (self.toolBar) {
            self.toolBar.y = kScreenHeight - self.height - self.toolBar.height;
        }
    } completion:^(BOOL finished) {
    }];
}


- (void)delete
{
    
    if (self.responderLable && [self.responderLable respondsToSelector:@selector(text)] && ((UILabel *)self.responderLable).text.length) {
        
        
        ((UILabel *)self.responderLable).text = [((UILabel *)self.responderLable).text substringToIndex:((UILabel *)self.responderLable).text.length-1];
    }
}

- (void)ok
{
    
    [self dissmiss];
}

- (void)numberWithText:(NSString *)text
{
    if (self.responderLable && [self.responderLable respondsToSelector:@selector(text)]) {
         ((UILabel *)self.responderLable).text = [((UILabel *)self.responderLable).text stringByAppendingString:text];
    }
}


#pragma mark - delegate
//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    CGFloat height = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    NSLog(@"------------userInfo:%@", userInfo);
    if (height > 0) {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView animateWithDuration:duration animations:^{
            if (self.toolBar) {
                self.toolBar.y = kScreenHeight - height - self.toolBar.height;
            }
        } completion:^(BOOL finished) {
        }];
    }
    
}

//当键退出
- (void)keyboardWillHide:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
//    CGFloat height = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView animateWithDuration:duration animations:^{
        if (self.toolBar) {
            self.toolBar.y = kScreenHeight - self.height - self.toolBar.height;
        }
    } completion:^(BOOL finished) {
    }];
}


- (void)KeyboardNumPadDidClickedWith:(NSString *)str buttonType:(KeyBoardButtonType)buttonType
{
    if (buttonType == KeyBoardButtonTypeDate) {
        
        if ([self.delegate respondsToSelector:@selector(keyboardDateDidClickedWith:)]) {
            [self.delegate keyboardDateDidClickedWith:self];
        }
    }else if (buttonType == KeyBoardButtonTypeDelete){
        
        [self delete];
    }else if (buttonType == KeyBoardButtonTypeOK){
        
        [self ok];
        if ([self.delegate respondsToSelector:@selector(keyboardDoneDidClickedWith:)]) {
            [self.delegate keyboardDoneDidClickedWith:self];
        }
    }else if (buttonType == KeyBoardButtonTypePoint){
        
        [self numberWithText:str];
    }else if (buttonType == KeyBoardButtonTypeNumber){
        
        [self numberWithText:str];
    }
}

- (void)dealloc
{
    NSLog(@"delloc_QHKeyboard");
     [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - setter && getter
- (void)setDate:(NSDate *)date
{
    self.numPad.date = date;
}

- (NSDate *)date
{
    return self.numPad.date;
}

@end

